package br.com.locasis.conversores;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

import br.com.locasis.utilitarios.UtilString;

@org.jboss.seam.annotations.faces.Converter(id="conversorCEP")
public class ConversorCEP implements Converter {

	public Object getAsObject(FacesContext context, UIComponent component, String valorCampo){
		Object cep = null;
		if(valorCampo != null) {
			if(!UtilString.isStringVazia(valorCampo)){
				cep = valorCampo;
			}
		}
		return cep;
	}

	public String getAsString(FacesContext context, UIComponent component, Object objCampo){
		String cep = null;
		if(objCampo != null) {
			if(!UtilString.isStringVazia(objCampo.toString())){
				cep = objCampo.toString();
			}
		}
		return cep;
	}
	
	public static Object desmascara(String cep) {
       cep = UtilString.removeAll('-', cep);
       return cep;
    }

    public static String mascara(Object cep) {
	   StringBuffer stringBuffer = new StringBuffer(cep.toString());
	   stringBuffer.insert(5, "-");                        
	   return stringBuffer.toString();
    }
	
}
